# -*- coding:UTF-8 -*-
from selenium import webdriver
import time
from openpyxl import Workbook
import threading
import figure_data_deal
import SplitWord
import re


# 获取文本中的时间字符串
def get_ymd(str_ymd):
    pattern_ymd = re.compile(r'\d+年\d+月\d+日')  # 定义匹配模式
    list = re.findall(pattern_ymd, str_ymd)
    if list.__len__() > 0:
        return list[0]
    else:
        pattern_ym = re.compile(r'\d+年\d+月')
        list = re.findall(pattern_ym, str_ymd)
        if list.__len__() > 0:
            return list[0]
        else:
            pattern_y = re.compile(r'\d+年')
            list = re.findall(pattern_y, str_ymd)
            if list.__len__() > 0:
                return list[0]
            else:
                return ''


# 将数据存入按特定的格式存入excel文件中
def save_to_excel(history_figures):
    try:
        wb = Workbook()
        ws1 = wb.active
        headers = ['类型', '人物名',
                   '人物属性',
                   '年龄',
                   '性别',
                   '出生时间',
                   '出生地',
                   '籍贯',
                   '人物经度',
                   '人物纬度',
                   '死亡时间',
                   '肖像',
                   '人物总结',
                   '地点时间',
                   '地点名',
                   '经度',
                   '纬度',
                   '排序',
                   '地点图片',
                   '地点描述']
        # 表头
        for h in range(len(headers)):
            ws1.cell(row=1, column=h + 1).value = headers[h]
        # 记录 地点行数
        record = 0
        # 填充行
        for h in range(len(history_figures)):
            figure = history_figures[h]
            h = record + h
            ws1.cell(row=h + 2, column=1).value = figure['types']
            ws1.cell(row=h + 2, column=2).value = figure['figure_name']
            ws1.cell(row=h + 2, column=3).value = figure['figure_type']
            ws1.cell(row=h + 2, column=4).value = figure['age']
            ws1.cell(row=h + 2, column=5).value = figure['sex']
            ws1.cell(row=h + 2, column=6).value = figure['birth_time']
            ws1.cell(row=h + 2, column=7).value = figure['birth_place']
            ws1.cell(row=h + 2, column=8).value = figure['hometown']
            ws1.cell(row=h + 2, column=9).value = figure['figure_lng']
            ws1.cell(row=h + 2, column=10).value = figure['figure_lat']
            ws1.cell(row=h + 2, column=11).value = figure['death_time']
            ws1.cell(row=h + 2, column=12).value = figure['avatar_src']
            ws1.cell(row=h + 2, column=13).value = figure['figure_summary']
            locations = figure['location']
            if len(locations) > 0:
                record = record + len(locations)
                for l in range(len(locations)):
                    location = locations[l]
                    ws1.cell(row=h + 3 + l, column=1).value = location['types']
                    ws1.cell(row=h + 3 + l, column=14).value = get_ymd(location['location_desc'])
                    ws1.cell(row=h + 3 + l, column=15).value = SplitWord.get_loc_name(location['location_desc'])
                    ws1.cell(row=h + 3 + l, column=16).value = location['location_lng']
                    ws1.cell(row=h + 3 + l, column=17).value = location['location_lat']
                    ws1.cell(row=h + 3 + l, column=18).value = location['sort']
                    ws1.cell(row=h + 3 + l, column=19).value = location['location_img']
                    ws1.cell(row=h + 3 + l, column=20).value = location['location_desc']
        # 保存
        wb.save(str(time.time()) + ".xlsx")
    except Exception as e:
        print(e)


# selenium初始化
def selenium_init(url, driver_path):
    # webdriver 驱动路径
    # path = 'E:/desktop/chromedriver_win32/chromedriver.exe'
    driver = webdriver.Chrome(executable_path=driver_path)
    driver.get(url)
    return driver


# 转化时间字符串为 YY-MM-dd的形式
def format_time(str_time):
    my_time = ''
    if str_time.find('（'):
        my_time = str_time.split('（')[0]
    else:
        my_time = str_time
    try:
        if my_time.find('日') != -1:
            array = time.strptime(my_time, u"%Y年%m月%d日")
            new_time = time.strftime("%Y-%m-%d", array)
            return new_time
        if my_time.find('月') != -1:
            array = time.strptime(my_time, u"%Y年%m月")
            new_time = time.strftime("%Y-%m", array)
            return new_time
        if my_time.find('年') != -1:
            array = time.strptime(my_time, u"%Y年")
            new_time = time.strftime("%Y", array)
            return new_time
    except Exception as e:
        return ''


# 获取时间中的年份
def strptime(t_str):
    try:
        return time.strptime(t_str, '%Y').tm_year
    except Exception as e1:
        try:
            return time.strptime(t_str, '%Y-%m').tm_year
        except Exception as e2:
            try:
                return time.strptime(t_str, '%Y-%m-%d').tm_year
            except Exception as e3:
                return -1


# 将人物姓名输入百度百科以获取人物的详细信息
def get_figure_info(figure_names, driver_path):
    url = 'https://baike.baidu.com/'
    driver = selenium_init(url, driver_path)
    figures = []
    for figure_name in figure_names:
        figure = {
            'types': '',
            'figure_type': '',
            'sex': '',
            'figure_name': '',
            'birth_time': '',
            'death_time': '',
            'birth_place': '',
            'hometown': '',
            'age': '',
            'avatar_src': '',
            'figure_summary': '',
            # 经度
            'figure_lng': '',
            # 纬度
            'figure_lat': '',
            'location': []
        }
        input = driver.find_element_by_id('query')
        input.click()
        input.clear()
        input.send_keys(figure_name)
        search = driver.find_element_by_id('search')
        search.click()
        try:
            # 类型
            figure['types'] = 'figure'
            # 姓名
            figure['figure_name'] = figure_name
            print(figure_name)

            titles = driver.find_elements_by_css_selector('dt[class="basicInfo-item name"]')
            values = driver.find_elements_by_css_selector('dd[class="basicInfo-item value"]')
            for i in range(len(titles)):
                if titles[i].text == '出生日期' or titles[i].text == '出生时间':
                    birth_time = values[i].text
                    birth_time = format_time(birth_time)
                    if not birth_time is None:
                        # 出生日期
                        figure['birth_time'] = birth_time
                        continue
                if titles[i].text == '逝世日期' or titles[i].text == '去世时间':
                    death_time = values[i].text
                    death_time = format_time(death_time)
                    if not death_time is None:
                        # 逝世日期
                        figure['death_time'] = death_time
                        continue
                if titles[i].text == '出生地':
                    # 出生地
                    figure['birth_place'] = values[i].text
                if titles[i].text.replace(' ', '') == '籍贯':
                    # 籍贯
                    figure['hometown'] = values[i].text
            try:
                # 年龄
                figure['age'] = strptime(figure['death_time']) - strptime(figure['birth_time'])
            except Exception as age_e:
                print(age_e)

            try:
                avatar = driver.find_element_by_css_selector(
                    'body > div.body-wrapper > div.content-wrapper > div > div.side-content > div.summary-pic > a > img')
                avatar_src = avatar.get_attribute('src')
                # 头像
                figure['avatar_src'] = avatar_src
            except Exception as avatar_exce:
                print(avatar_exce)

            summary = driver.find_element_by_css_selector(
                'body > div.body-wrapper > div.content-wrapper > div > div.main-content > div.lemma-summary').text
            # 人物总结
            figure['figure_summary'] = summary

            # 人物经历，经过的地点
            experiences = driver.find_elements_by_css_selector(
                "body > div.body-wrapper > div.content-wrapper > div > div.main-content  div.lemma-summary ~ div.para")
            expe = []
            for it in range(len(experiences)):
                locations = {
                    'types': '',
                    'location_time': '',
                    'location_name': '',
                    'location_lng': '',
                    'location_lat': '',
                    'sort': '',
                    'location_img': '',
                    'location_desc': ''
                }
                locations['types'] = 'location'
                locations['location_desc'] = experiences[it].text
                locations['sort'] = it + 1
                expe.append(locations)
            figure['location'] = expe

            figures.append(figure)
            driver.back()
        except Exception  as e:
            print(e)
            driver.back()
            continue
    save_to_excel(figures)


# list拆分
def split_list(x, n, newList=[]):
    if len(x) <= n:
        newList.append(x)
        return newList
    else:
        newList.append(x[:n])
        return split_list(x[n:], n)


driver_paths = []
a  = 0
for i in range(8):
    a = a + 1
    driver_paths.append('D:\desktop\chromedriver' + '-' + str(a) + '\chromedriver.exe')

names = figure_data_deal.get_all_figure_names()
new_names = split_list(names, 300)
threads = []
for it in range(len(new_names)):
    # 开启多线程
    t = threading.Thread(target=get_figure_info, args=(new_names[it], driver_paths[it]))
    threads.append(t)
    t.start()
